<?php 
    $date_formats = array(
        'd/m/Y' => 'dd/mm/yy',
        'Y/m/d' => 'yy/mm/dd',
    );
    $date_format = $date_formats[$langs[$lang]->date_format];
?>

<script src="<?php echo $url_styles ?>js/plugins/formselements/jquery.maskedinput-1.3.min.js"></script>
<script src="<?php echo $url_styles ?>js/plugins/formselements/ui.spinner.min.js"></script>
<script src="<?php echo $url_styles ?>js/plugins/gallery/jquery.colorbox-min.js"></script>
<script>

    //	$('.form-horizontal [name="price"]').spinner({ min: 0,numberFormat: 'n',step: 0.01});
    //	$('.form-horizontal [name="sale_price"]').spinner({ min: 0,numberFormat: 'n'});
    $('.form-horizontal [name="start"]').datepicker();
    $('.form-horizontal [name="end"]').datepicker();
    $('.form-horizontal [name="start"]').datepicker('option','dateFormat','<?php echo $date_format?>');
    $('.form-horizontal [name="end"]').datepicker('option','dateFormat','<?php echo $date_format?>');
    var image_index=0;
    $('#add_image').live('click',function(){
        image_index++;
        $('#images').append('<div><input type="file" name="images_'+image_index+'" class="input-xlarges"/>\n\
							<a href="javascript:;" class="delete_image"><?php echo lang('delete')?></a></div>');
        return false;
    });
    $('.delete_image').live('click',function(){
        $(this).parent().fadeOut('fast',function(){$(this).remove()});
    });
    $('#tab4_check').click(function(){
        $('.form-horizontal [name="catalogues[]"]').attr('checked',this.checked);
    })
    $('#tab5_check').click(function(){
        $('.form-horizontal [name="locations[]"]').attr('checked',this.checked);
    })
    $(document).ready(function(){
        var product = jQuery.parseJSON('<?php echo $product?>');
        //alert(product.thumb);
        $('.form-horizontal .product_image').attr('src',product.thumb);
        $('.form-horizontal .product_image').attr('style','max-width:120px;max-height:120px');
        $('.form-horizontal [name="id"]').val(product.id);
        $('.form-horizontal [name="price"]').val(product.price);
        $('.form-horizontal [name="sale_price"]').val(product.sale_price);
        $('.form-horizontal [name="start"]').val(product.start);
        $('.form-horizontal [name="end"]').val(product.end);
        for(var i in product.info){
            var info = product.info[i];
            $('.form-horizontal [name="info['+info.lang_id+'][name]"]"').val(info.name);
            $('.form-horizontal [name="info['+info.lang_id+'][description]"]"').val(info.description);
            $('.form-horizontal [name="info['+info.lang_id+'][meta_desc]"]"').val(info.meta_desc);
            $('.form-horizontal [name="info['+info.lang_id+'][meta_keyword]"]"').val(info.meta_keyword);
            $('.form-horizontal [name="info['+info.lang_id+'][advantages]"]"').val(info.advantages);
        }
        for(var i in product.catalogues){
            var cat_id = product.catalogues[i].cat_id;
            $('.form-horizontal [name="catalogues[]"][value="'+cat_id+'"]').attr('checked',true);
        }
        for(var i in product.locations){
            var location_id = product.locations[i].location_id;
            $('.form-horizontal [name="locations[]"][value="'+location_id+'"]').attr('checked',true);
        }
        var img_gallery = '';
        for(var i in product.images)
        {
            var img=product.images[i];
            img_gallery+='<li>'+
                    '<img src="'+img.thumb+'" alt="placeholder">'+
                    '<div>'+
                    '<ul style="width:67px">'+
                    '<li class="edit"><a href="'+img.path+'"><?php echo lang('view')?></a></li>'+
                    '<li class="delete" data="'+img.id+'"><a href="#"><?php echo lang('delete')?></a></li>'+
                    '</ul>'+
                    '</div>'+
                    '</li>';
        }
        $('#basicGallery').html(img_gallery);
        var error = jQuery.parseJSON('<?php echo $error?>');
        for(var i in error)
        {
            if(i=='id' || i=='catalogues' || i=='locations')
            {

            }
            else if(error[i]!=''){
                $('.form-horizontal [name="'+i+'"]').after('<span class="help-inline">'+error[i]+'</span>');
                $('.form-horizontal [name="'+i+'"]').parent().parent().addClass('error');
            }

        }

    });
    jQuery(function($) {

        /* --->> 1 - Basic gallery options show/hide --------------*/
        $("#basicGallery > li").hoverIntent(
                function () {
                    $(this).find("div").fadeIn("fast");
                },
                function () {
                    $(this).find("div").fadeOut("fast");
                }
        );

        /* --->> 2 - Basic gallery setup --------------*/
        $("#basicGallery .edit a").colorbox({rel:'gal'});

        /* --->> 3 - Delete images --------------*/
        $("#basicGallery .delete").click(function(){
            if(!confirm('<?php echo lang('delete_confirm')?>')) return false;
            $.ajax({
                url: '<?php echo url_create('admin','product','delete_image')?>',
                type: 'POST',
                dataType: 'json',
                data: {id: $(this).attr('data')},
                success: function(o)
                {
                    if(o.state=='success')
                        $('#basicGallery li [data="'+ o.id+'"]').fadeOut().remove();
                    else
                        $.jGrowl(o.message, {
                            header: o.title,
                            theme:"notification_styled_"+o.state
                        });
                }

            })
            return false;
        });

    });
</script>
